#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    int maximalNetworkRank(int n, vector<vector<int>> &roads)
    {
        vector<vector<bool>> a(n, vector<bool>(n, false));
        vector<int> degree(n, 0);
        for (const auto &e : roads)
        {
            a[e[0]][e[1]] = true;
            a[e[1]][e[0]] = true;
            degree[e[0]]++;
            degree[e[1]]++;
        }
        int ret = 0;
        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                int tmp = 0;
                if (a[i][j])
                {
                    tmp = degree[i] + degree[j] - 1;
                }
                else
                {
                    tmp = degree[i] + degree[j];
                }
                ret = max(tmp, ret);
            }
        }
        return ret;
    }
};